package com.softhinker.ke24.unit3;

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.SwingConstants;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;

import jess.JessException;

import com.softhinker.ke24.unit3.engine.InferenceEngine;
import com.softhinker.ke24.unit3.engine.QuestionProvider;
import com.softhinker.ke24.unit3.ui.DetailsPanel;
import com.softhinker.ke24.unit3.ui.QuestionPanel;
import com.softhinker.ke24.unit3.ui.ResultPanel;

public class MainFace extends JFrame {

	private static final long serialVersionUID = 4675878742957980719L;

	public MainFace() throws JessException {
		try {
		    for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
		        if ("Nimbus".equals(info.getName())) {
		            UIManager.setLookAndFeel(info.getClassName());
		            break;
		        }
		    }
		} catch (Exception e) {
		    System.err.println(e.getMessage());
		}
		
		// Center the window
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int height = screenSize.height;
		int width = screenSize.width;
		this.setSize(width / 2, height / 2);
		this.setLocationRelativeTo(null);
		this.setMinimumSize(new Dimension(700, 600));
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		this.setTitle("KE-U3-CA : Stock Advisor");
		init();
		this.pack();
		this.setVisible(true);
	}
	
	private void init() throws JessException{
		JPanel panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.LINE_AXIS));
		 
		JPanel stocksPanel = new JPanel();
		stocksPanel.add(new JLabel("Stocks"));
		
		DetailsPanel stockDetailsPanel = new DetailsPanel();
		
		JPanel rightPanel = new JPanel();
		rightPanel.setLayout(new BoxLayout(rightPanel, BoxLayout.PAGE_AXIS));
		ResultPanel resultPanel = new ResultPanel(stockDetailsPanel);
		rightPanel.add(resultPanel);
		rightPanel.add(new JSeparator(SwingConstants.HORIZONTAL));
		rightPanel.add(stockDetailsPanel);
		
		QuestionProvider questionProvider = new QuestionProvider();
		InferenceEngine engine = new InferenceEngine();
		engine.setResultListener(resultPanel);
		questionProvider.addResultListener(engine);
		
		panel.add(new QuestionPanel(questionProvider));
		panel.add(new JSeparator(SwingConstants.VERTICAL));
		panel.add(rightPanel);
		panel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		
		this.add(panel);
	}
}
